/*
 * Copyright GTEAM.,LTD.
 * All rights reserved.
 * 
 * This software is the confidential and proprietary information
 * of yysvip.tistory.com.,LTD. ("Confidential Information").
 */
package egovframework.admin.web; 

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springmodules.validation.commons.DefaultBeanValidator;

import egovframework.com.bean.DefaultVO;
import egovframework.com.bean.GoodsVO;
import egovframework.com.session.AdminSession;
import egovframework.com.util.Util;
import egovframework.goods.service.GoodsService;
import egovframework.rte.fdl.property.EgovPropertyService;
import egovframework.rte.ptl.mvc.tags.ui.pagination.PaginationInfo;

/**
 * <pre>
 * egovframework.admin.web 
 *    └ AdminGoodsController.java
 * 
 * </pre>
 * @date : 2014. 10. 6. 오후 11:33:37
 * @author : us
 * @history : 
 *	-----------------------------------------------------------------------
 *	변경일				작성자						변경내용  
 *	----------- ------------------- ---------------------------------------
 *	2014. 10. 6.		nimbi				최초 작성 
 *	-----------------------------------------------------------------------
 * 
 */
@Controller
public class AdminGoodsController extends AdminSession {
	
	/** 상품 서비스 **/
	@Resource(name = "goodsService")
	private GoodsService goodsService;
	
	/** 유티릴티 모듈 */
    @Resource(name = "util")
    private Util util;
    
	/** 프로퍼티 서비스 */
    @Resource(name = "propertiesService")
    protected EgovPropertyService propertiesService;
    
    /** Validator */
    @Resource(name = "beanValidator")
	protected DefaultBeanValidator beanValidator;
    
	private Logger log = Logger.getLogger(this.getClass());
	
	/**
	 * @Method : selectGoodsCateList
	 * @Annotate : 상품 카테고리 목록
	 * @date : 2014. 10. 7.
	 * @author : us
	 * @history : 
	 *	-----------------------------------------------------------------------
	 *	변경일				작성자						변경내용  
	 *	----------- ------------------- ---------------------------------------
	 *	2014. 10. 7.		nimbi				최초 작성 
	 *	-----------------------------------------------------------------------
	 */ 	
	@RequestMapping(value = "/ADMIN/GoodsCategory.do")
	public String selectGoodsCateList(
			@ModelAttribute("goodsVO") GoodsVO goodsVO,
			HttpServletRequest request,
			ModelMap model,
			HttpSession session
	) throws Exception {
		if(!isAdmin(session, request)) {
			model.addAttribute("errMsg", "error.auth.fail");
			return "/common/error";
		}
		GoodsVO goodscate1VO = new GoodsVO();
		goodscate1VO.setTablename(propertiesService.getString("cate1tbl"));
		goodscate1VO.setSearchCondition(propertiesService.getString("cate1useyn"));
		goodscate1VO.setOrderby(propertiesService.getString("cate1seq"));
		List<?> cate1List = goodsService.selectGoodsCateList(goodscate1VO);
		model.addAttribute("cate1List", cate1List);
		
		if(!util.chkZero(goodsVO.getGcaSn()) && !util.chkInt(goodsVO.getGcaSn())) {
			GoodsVO goodscate2VO = new GoodsVO();
			goodscate2VO.setTablename(propertiesService.getString("cate2tbl"));
			goodscate2VO.setSearchCondition(propertiesService.getString("cate2useyn"));
			goodscate2VO.setOrderby(propertiesService.getString("cate2seq"));
			goodscate2VO.setSearchColumn("GCA_SN");
			goodscate2VO.setSearchKeyword(Integer.toString(goodsVO.getGcaSn()));
			List<?> cate2List = goodsService.selectGoodsCateList(goodscate2VO);
			model.addAttribute("cate2List", cate2List);
		}
		
		if(!util.chkZero(goodsVO.getGcbSn()) && !util.chkInt(goodsVO.getGcbSn())) {
			GoodsVO goodscate3VO = new GoodsVO();
			goodscate3VO.setTablename(propertiesService.getString("cate3tbl"));
			goodscate3VO.setSearchCondition(propertiesService.getString("cate3useyn"));
			goodscate3VO.setOrderby(propertiesService.getString("cate3seq"));
			goodscate3VO.setSearchColumn("GCA_SN");
			goodscate3VO.setSearchKeyword(Integer.toString(goodsVO.getGcaSn()));
			goodscate3VO.setSearchColumn2("GCB_SN");
			goodscate3VO.setSearchKeyword2(Integer.toString(goodsVO.getGcbSn()));
			List<?> cate3List = goodsService.selectGoodsCateList(goodscate3VO);
			model.addAttribute("cate3List", cate3List);
		}
		
		goodsVO.setPageIndex(goodsVO.getPageIndex());
		goodsVO.setPageUnit(propertiesService.getInt("pageUnit"));
		goodsVO.setPageSize(propertiesService.getInt("pageSize"));
    	PaginationInfo paginationInfo = new PaginationInfo();
		paginationInfo.setCurrentPageNo(goodsVO.getPageIndex());
		paginationInfo.setRecordCountPerPage(goodsVO.getPageUnit());
		paginationInfo.setPageSize(goodsVO.getPageSize());
		goodsVO.setFirstIndex(paginationInfo.getFirstRecordIndex());
		goodsVO.setLastIndex(paginationInfo.getLastRecordIndex());
		goodsVO.setRecordCountPerPage(paginationInfo.getRecordCountPerPage());
		List<?> cateList = goodsService.selectGoodsCate(goodsVO);
		model.addAttribute("cateList", cateList);
		int cateCount = goodsService.selectGoodsCateCount(goodsVO);
		paginationInfo.setTotalRecordCount(cateCount);
		model.addAttribute("pageInfo", paginationInfo);
		return "/admin/03goods/goods_cate_list";
	}
	
	/**
	 * @Method : writeGoodsCate
	 * @Annotate : 상품 카테고리 등록작성
	 * @date : 2014. 10. 7.
	 * @author : nimbi
	 * @history : 
	 *	-----------------------------------------------------------------------
	 *	변경일				작성자						변경내용  
	 *	----------- ------------------- ---------------------------------------
	 *	2014. 10. 7.		nimbi				최초 작성 
	 *	-----------------------------------------------------------------------
	 */ 	
	@RequestMapping(value = "/ADMIN/GoodsCateWrite.do", method = RequestMethod.GET)
	public String writeGoodsCate(
			@ModelAttribute("goodsVO") GoodsVO goodsVO,
			HttpSession session,
			HttpServletRequest request,
			ModelMap model
	) throws Exception {
		if(!isAdmin(session, request)) {
			model.addAttribute("errMsg", "error.auth.fail");
			return "/common/error";
		}
		if(goodsVO.getDepth() >= 2) {
			goodsVO.setTablename(propertiesService.getString("cate1tbl"));
			goodsVO.setSearchCondition(propertiesService.getString("cate1useyn"));
			goodsVO.setOrderby(propertiesService.getString("cate1seq"));
			List<?> cate1List = goodsService.selectGoodsCateList(goodsVO);
			model.addAttribute("cate1List", cate1List);
		}
		if(!util.doNull(Integer.toString(goodsVO.getGcaSn())).equals("") && goodsVO.getDepth() >= 3) {
			goodsVO.setTablename(propertiesService.getString("cate2tbl"));
			goodsVO.setSearchCondition(propertiesService.getString("cate2useyn"));
			goodsVO.setOrderby(propertiesService.getString("cate2seq"));
			goodsVO.setSearchColumn("GCA_SN");
			goodsVO.setSearchKeyword(Integer.toString(goodsVO.getGcaSn()));
			List<?> cate2List = goodsService.selectGoodsCateList(goodsVO);
			model.addAttribute("cate2List", cate2List);
		}
		model.addAttribute("goodscateVO", new GoodsVO());
		return "/admin/03goods/goods_cate_write";
	}
	
	/**
	 * @Method : insertGoodsCate
	 * @Annotate : 상품 카테고리 등록처리
	 * @date : 2014. 10. 8.
	 * @author : nimbi
	 * @history : 
	 *	-----------------------------------------------------------------------
	 *	변경일				작성자						변경내용  
	 *	----------- ------------------- ---------------------------------------
	 *	2014. 10. 8.		nimbi				최초 작성 
	 *	-----------------------------------------------------------------------
	 */ 	
	@RequestMapping(value = "/ADMIN/GoodsCateInsert.do", method = RequestMethod.GET)
	public String getInsertGoodsCate() throws Exception {
		return "redirect:/ADMIN/GoodsCategory.do";
	}
	@RequestMapping(value = "/ADMIN/GoodsCateInsert.do", method = RequestMethod.POST)
	public String insertGoodsCate(
			GoodsVO goodscateVO,
			BindingResult bindingResult,
			HttpSession session,
			HttpServletRequest request,
			ModelMap model,
			SessionStatus status
	) throws Exception {
		if(!isAdmin(session, request)) {
			model.addAttribute("errMsg", "error.auth.fail");
			return "/common/error";
		}
		beanValidator.validate(goodscateVO, bindingResult);
    	if (bindingResult.hasErrors()) {
    		model.addAttribute("errMsg","error.validate.fail");
    		return "/admin/03goods/goods_cate_write";
    	}
    	goodscateVO.setTablename(propertiesService.getString("cate" + Integer.toString(goodscateVO.getDepth()) + "tbl"));
		if(goodscateVO.getDepth() == 1) {
			goodscateVO.setGcaSeq(goodsService.selectMaxSeq(goodscateVO) + 1);
			int gcaSn = goodsService.selectMaxSn(goodscateVO);
			if(Integer.toString(gcaSn) == null || gcaSn == 0)
				goodscateVO.setGcaSn(propertiesService.getInt("minSn") + 1000000);
			else goodscateVO.setGcaSn(gcaSn + 1000000);
			goodscateVO.setGcaName(goodscateVO.getCateName());
		}
		else if(goodscateVO.getDepth() == 2) {
			goodscateVO.setGcbSeq(goodsService.selectMaxSeq(goodscateVO) + 1);
			int gcbSn = goodsService.selectMaxSn(goodscateVO);
			if(Integer.toString(gcbSn) == null || gcbSn == 0)
				goodscateVO.setGcbSn(goodscateVO.getGcaSn() + 1000);
			else goodscateVO.setGcbSn(gcbSn + 1000);
			System.out.println(goodscateVO.getGcaSeq());
			goodscateVO.setGcbName(goodscateVO.getCateName()); 
		}
		else if(goodscateVO.getDepth() == 3) {
			goodscateVO.setGccSeq(goodsService.selectMaxSeq(goodscateVO) + 1);
			int gccSn = goodsService.selectMaxSn(goodscateVO);
			if(Integer.toString(gccSn) == null || gccSn == 0)
				goodscateVO.setGccSn(goodscateVO.getGcbSn() + 1);
			else goodscateVO.setGccSn(gccSn + 1);
			goodscateVO.setGccName(goodscateVO.getCateName()); 
		}
		goodsService.insertGoodsCate(goodscateVO);
		status.setComplete();
		return "redirect:/ADMIN/GoodsCategory.do";
	}
	
	/**
	 * @Method Name : deleteGoodsCate
	 * @Annotate : 카테고리 삭제처리
	 * @date : 2014. 10. 11.
	 * @author : nimbi
	 * @history : 
	 *	-----------------------------------------------------------------------
	 *	변경일				작성자						변경내용  
	 *	----------- ------------------- ---------------------------------------
	 *	2014. 10. 11.		nimbi				최초 작성 
	 *	-----------------------------------------------------------------------
	 */ 	
	@RequestMapping(value = "/ADMIN/GoodsCateDelete.do")
	public String deleteGoodsCate(
			@ModelAttribute("goodscateVO") GoodsVO goodscateVO,
			BindingResult bindingResult,
			HttpSession session,
			HttpServletRequest request,
			ModelMap model,
			SessionStatus status
	) throws Exception {
		if(goodscateVO.getDepth() == 1) {
			goodscateVO.setTablename(propertiesService.getString("cate3tbl"));
			goodscateVO.setSearchCondition(propertiesService.getString("cate3useyn"));
			goodscateVO.setOrderby(propertiesService.getString("cate3seq"));
			goodscateVO.setSearchCondition("GCC_USE_YN");
			goodscateVO.setSearchColumn("GCA_SN");
			goodscateVO.setSearchKeyword(Integer.toString(goodscateVO.getGcSn()));
			goodsService.deleteGoodsCate(goodscateVO);
			
			goodscateVO.setTablename(propertiesService.getString("cate2tbl"));
			goodscateVO.setSearchCondition(propertiesService.getString("cate2useyn"));
			goodscateVO.setOrderby(propertiesService.getString("cate2seq"));
			goodscateVO.setSearchCondition("GCB_USE_YN");
			goodscateVO.setSearchColumn("GCA_SN");
			//goodscateVO.setSearchKeyword(Integer.toString(goodscateVO.getGcaSn()));
			goodsService.deleteGoodsCate(goodscateVO);		
			
			goodscateVO.setTablename(propertiesService.getString("cate1tbl"));
			goodscateVO.setSearchCondition(propertiesService.getString("cate1useyn"));
			goodscateVO.setOrderby(propertiesService.getString("cate1seq"));
			goodscateVO.setSearchCondition("GCA_USE_YN");
			goodscateVO.setSearchColumn("GCA_SN");
			//goodscateVO.setSearchKeyword(Integer.toString(goodscateVO.getGcaSn()));
			goodsService.deleteGoodsCate(goodscateVO);
		}
		else if(goodscateVO.getDepth() == 2) {
			goodscateVO.setTablename(propertiesService.getString("cate3tbl"));
			goodscateVO.setSearchCondition(propertiesService.getString("cate3useyn"));
			goodscateVO.setOrderby(propertiesService.getString("cate3seq"));
			goodscateVO.setSearchCondition("GCC_USE_YN");
			goodscateVO.setSearchColumn("GCA_SN");
			goodscateVO.setSearchKeyword(Integer.toString(goodscateVO.getGcSn()));
			goodsService.deleteGoodsCate(goodscateVO);
			
			goodscateVO.setTablename(propertiesService.getString("cate2tbl"));
			goodscateVO.setSearchCondition(propertiesService.getString("cate2useyn"));
			goodscateVO.setOrderby(propertiesService.getString("cate2seq"));
			goodscateVO.setSearchCondition("GCB_USE_YN");
			goodscateVO.setSearchColumn("GCA_SN");
			//goodscateVO.setSearchKeyword(Integer.toString(goodscateVO.getGcaSn()));
			goodsService.deleteGoodsCate(goodscateVO);		
		}
		else {
			goodscateVO.setTablename(propertiesService.getString("cate3tbl"));
			goodscateVO.setSearchCondition(propertiesService.getString("cate3useyn"));
			goodscateVO.setOrderby(propertiesService.getString("cate3seq"));
			goodscateVO.setSearchCondition("GCC_USE_YN");
			goodscateVO.setSearchColumn("GCA_SN");
			goodscateVO.setSearchKeyword(Integer.toString(goodscateVO.getGcSn()));
			goodsService.deleteGoodsCate(goodscateVO);
		}
		status.setComplete();
		return "redirect:/ADMIN/GoodsCategory.do";
	}
	
	@RequestMapping(value = "/ADMIN/GoodsCateRestore.do")
	public String restoreGoodsCate(
			@ModelAttribute("goodscateVO") GoodsVO goodscateVO,
			BindingResult bindingResult,
			HttpSession session,
			HttpServletRequest request,
			ModelMap model,
			SessionStatus status
	) throws Exception {
		goodscateVO.setTablename(propertiesService.getString("cate" + goodscateVO.getDepth() + "tbl"));
		goodscateVO.setSearchCondition(propertiesService.getString("cate" + goodscateVO.getDepth() + "useyn"));
		goodscateVO.setOrderby(propertiesService.getString("cate" + goodscateVO.getDepth() + "seq"));
		goodscateVO.setSearchKeyword(Integer.toString(goodscateVO.getGcSn()));
		
		if(goodscateVO.getDepth() == 1) {
			goodscateVO.setSearchCondition("GCA_USE_YN");
			goodscateVO.setSearchColumn("GCA_SN");
		}
		else if(goodscateVO.getDepth() == 2) {
			goodscateVO.setSearchCondition("GCB_USE_YN");
			goodscateVO.setSearchColumn("GCB_SN");
		}
		else {
			goodscateVO.setSearchCondition("GCC_USE_YN");
			goodscateVO.setSearchColumn("GCC_SN");
		}
		goodsService.restoreGoodsCate(goodscateVO);
		status.setComplete();
		return "redirect:/ADMIN/GoodsCategory.do";
	}
}
